package cc.fs.myproject;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.widget.LinearLayout;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cc.fs.myproject.fragments.FourFragment;
import cc.fs.myproject.fragments.OneFragment;
import cc.fs.myproject.fragments.ThreeFragment;
import cc.fs.myproject.fragments.TwoFragment;
import cc.fs.myproject.utils.AlertDialogShowHelper;
import cc.fs.myproject.utils.BottomTabsHelper;
import cc.fs.myproject.utils.T;

public class MainActivity extends BaseActivity implements BottomTabsHelper.OnTagChangeLinstener{

    @InjectView(R.id.id_toolbar)
    Toolbar toolbar;
    @InjectView(R.id.bottomTabs)
    LinearLayout bottomTabs;

    private BottomTabsHelper bottomHelper;
    private FragmentManager fragmentManager;
    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    private FourFragment fourFragment;
    private int mPosition = 0;

    public static void start(Context context){
        Intent intent = new Intent(context,MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        toolbar.setTitle("Hello Android 5.0");
        toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        setSupportActionBar(toolbar);

        bottomHelper = BottomTabsHelper.newInstance(bottomTabs,this,0);
        bottomHelper.badgeView[1].setText("2");
        bottomHelper.badgeView[1].setBadgeBackgroundColor(0xff3C5896);
        bottomHelper.badgeView[1].show();

        initFragment();

    }

    private Fragment fragments[] = new Fragment[4];
    private String tags[] = new String[]{"one","two","three","four"};
    private Class[] classes = new Class[]{OneFragment.class,TwoFragment.class,ThreeFragment.class,FourFragment.class};


    void initFragment(){

        fragmentManager = this.getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        oneFragment = new OneFragment();
        twoFragment = new TwoFragment();
        threeFragment = new ThreeFragment();
        fourFragment = new FourFragment();

        for(int i=0;i<tags.length;i++){
            Fragment fragment = fragmentManager.findFragmentByTag(tags[i]);
            //解决出错后重新加载后的问题
            if(fragment != null && !fragment.isDetached()) {
                fragmentTransaction.detach(fragment);
            }

            //初始化
            fragment = Fragment.instantiate(this,classes[i].getName(),null);
            fragmentTransaction.add(R.id.fragmentLayout,fragment,tags[i]);
            fragments[i] = fragment;
        }

        //赋值
        oneFragment = (OneFragment)fragments[0];
        twoFragment = (TwoFragment)fragments[1];
        threeFragment = (ThreeFragment)fragments[2];
        fourFragment = (FourFragment)fragments[3];

        //提交事务
        fragmentTransaction.hide(twoFragment).hide(threeFragment).hide(fourFragment);
        fragmentTransaction.commit();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }else if(id == R.id.gaode){
            GaoDeMapActivity.start(this);
        }else if(id == R.id.retrofit){
            RetrofitActivity.start(this);
        }else if(id == R.id.rxAndroid){
            RxAndroidActivity.start(this);
        }else if(id == R.id.dialog){
            AlertDialog.Builder builder = new  AlertDialog.Builder(_this);
            builder.setTitle("提示")
                    .setMessage("测试dialog")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
            AlertDialog alertDialog = builder.create();
            alertDialog.setOnShowListener(new AlertDialogShowHelper(this));
            alertDialog.show();
        } else if(id == R.id.popwinow){
            PopWindowActivity.start(_this);
        } else if(id == R.id.animation){
            AnimationActivity.start(_this);
        }


        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTagChange(int position) {

        if(mPosition != position){
            FragmentTransaction fragmentTransaction = this.getFragmentManager().beginTransaction();
            fragmentTransaction.hide(fragments[mPosition]);
            fragmentTransaction.show(fragments[position]);
            fragmentTransaction.commit();
            mPosition = position;
        }

        switch (position){
            case 0:
                T.showShort(_this,"0");
                break;
            case 1:
                T.showShort(_this,"1");
                break;
            case 2:
                T.showShort(_this,"2");
                break;
            case 3:
                T.showShort(_this,"3");
                break;

        }

    }
}
